<?php

namespace app\api\controller;

use app\common\controller\Api;

use think\Db;

/**
 * 微信相关
 */
class Wechats extends Api
{
    protected $noNeedLogin = ['get_access_token', 'getWxInfoTest', 'getWechatScan', 'getWxConfigInfo', 'getWxOpenId'];
    protected $noNeedRight = '*';

    /**
     * 获取授权token网页授权
     */
    public static function get_access_token($code)
    {
        $wx_token = "";
        if (session('wx_token') && !empty(session('wx_token'))) {
            $wx_token = session("wx_token");
        } else {
            $wechat = config("wechat");
            $urls = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $wechat["AppID"] . "&secret=" . $wechat["AppSecret"];
            $token_data = http_curl($urls);
            dump($token_data);
            if (!empty($token_data)) {
                session("wx_token", $token_data["access_token"], $token_data["expires_in"]);
                $wx_token = $token_data["access_token"];
            }
        }
        return $wx_token;
    }

    /**
     * 获取微信用户信息
     */
    public static function getuserinfo($wx_token)
    {
        $urls = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $wx_token . "&openid=" . config("wechat")["AppID"] . "&lang=zh_CN";
        $token_data = http_curl($urls);
        return $token_data["openid"];
    }

    public static function getWxInfo($code)
    {
        $urls = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . config("wechat")["AppID"] . "&secret=" . config("wechat")["AppSecret"] . "&code=" . $code . "&grant_type=authorization_code";
        $token_data = http_curl($urls);
        if (isset($token_data["openid"])) {
            return $token_data["openid"];
        } else {
            return "";
        }
    }

    public function getWechatScan()
    {
        $wechat = config("wechat_fdjf");
        //获取accesstoken
        $token = get_accesstoken($wechat["AppID"], $wechat["AppSecret"]);
        //获取jsapi_ticket
        $ticket = get_jsapi_ticket($token);

        //整合参数
        $sign_data['noncestr'] = createNoncestr();
        $sign_data['jsapi_ticket'] = $ticket;
        $sign_data['timestamp'] = time();
        $sign_data['url'] = config("api_url") . "#/pages/index/exchange/exchange";

        //签名
        $signature = getSign($sign_data);

        $scancon['signature'] = $signature;
        $scancon['noncestr'] = $sign_data['noncestr'];
        $scancon['timestamp'] = $sign_data['timestamp'];
        $scancon['appid'] = $wechat["AppID"];

        $this->success("请求成功", $scancon);
    }

    // 获取公众号信息
    public function getWxConfigInfo()
    {
        $this->success("公众号信息", config("wechat_klyfw"));
    }

    // 获取微信OPENID
    public function getWxOpenId()
    {
        $code = $this->request->post("code");
        if (!empty($code)) {
            $openid = $this->getWxOpenIds($code, config("wechat_klyfw.AppID"), config("wechat_klyfw.AppSecret"));
            if (!empty($openid)) {
                $this->success("openid", $openid);
            } else {
                $this->error("获取失败");
            }
        } else {
            $this->error("参数错误");
        }
    }

    // 获取用户微信OPENID
    public function getWxOpenIds($code, $appid, $secret)
    {
        $openid_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $secret . '&code=' . $code . '&grant_type=authorization_code';
        $res = json_decode(file_get_contents($openid_url), true);
        if (isset($res['openid'])) {
            return $res['openid'];
        } else {
            return '';
        }
    }
}
